package com.lqq.eduservice.controller.admin;


import com.lqq.commonutils.R;
import com.lqq.eduservice.client.VodClient;
import com.lqq.eduservice.entity.EduVideo;
import com.lqq.eduservice.service.EduVideoService;
import com.lqq.servicebase.exceptionHandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-29
 */
@RestController
@RequestMapping("/eduservice/video")
//@CrossOrigin//解决跨域
public class EduVideoController {
    @Autowired
    private EduVideoService videoService;

    //添加小节
    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo) {
        videoService.save(eduVideo);
        return R.ok();
    }

    @Autowired
    private VodClient vodClient;
    //删除小节，也同时把阿里云的视频也删除了
    @DeleteMapping("/delete/{videoId}")
    public R deleteVideo(@PathVariable String videoId) {
        //根据小节id获取视频id
        EduVideo video = videoService.getById(videoId);
        String videoSourceId = video.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)) {
            //调用微服务edu，根据videoSourceId删除阿里云的视频
            R result = vodClient.removeAlyVideo(videoSourceId);
            if (result.getCode() == 20001) {
                throw new GuliException(20001,"删除视频失败，fallback...");
            }
        }
        //删除小节
        videoService.removeById(videoId);
        return R.ok();
    }

    //根据id查询小节
    @GetMapping("/getVideo/{videoId}")
    public R getVideo(@PathVariable String videoId) {
        EduVideo video = videoService.getById(videoId);
        return R.ok().data("video", video);
    }

    //编辑小节
    @PostMapping("/updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo) {
        videoService.updateById(eduVideo);
        return R.ok();
    }
}